package com.canbot.u05.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.canbot.u05.bean.CustomDialogue;

import java.util.ArrayList;

public class MyDbUtils {

        /**
         * 插入数据
         *
         * @param context
         * @param question
         * @param answer
         * @param type
         */
        public static void add(Context context, String question, String answer,
                               int type) {
                DatabaseHelper dbHelper = new DatabaseHelper(context, "voiceInfo_db");
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("question", question);
                values.put("answer", answer);
                values.put("type", type);
                long result = db.insert("custom_dialogue", null, values);
                if (result > 0) {
                        Log.e("DatabaseUtils", "插入成功！");
                        Log.e("DatabaseUtils", "插入条目：" + result);
                }

        }

        /**
         * 查询数据
         *
         * @param context
         * @return
         */
        public static ArrayList<CustomDialogue> queryAll(Context context) {
                ArrayList<CustomDialogue> datas = new ArrayList<CustomDialogue>();
                CustomDialogue data;
                DatabaseHelper dbHelper = new DatabaseHelper(context, "voiceInfo_db");
                SQLiteDatabase db = dbHelper.getReadableDatabase();

                Cursor cursor = db.query("custom_dialogue", null, null, null, null,
                        null, null, null);

                while (cursor.moveToNext()) {
                        String question = cursor
                                .getString(cursor.getColumnIndex("question"));
                        String answer = cursor.getString(cursor.getColumnIndex("answer"));
                        int type = cursor.getInt(cursor.getColumnIndex("type"));
                        int id = cursor.getInt(cursor.getColumnIndex("_id"));

                        Log.i("DatabaseUtils", "query-->question:" + question + ",answer"
                                + answer + ",type" + type);
                        data = new CustomDialogue(id, question, answer, type);
                        datas.add(data);
                }
                cursor.close();
                return datas;
        }

        /**
         * 删除数据
         *
         * @param context
         * @param answer
         * @param name
         */
        public static void delete(Context context, String question, String answer) {
                DatabaseHelper dbHelper = new DatabaseHelper(context, "voiceInfo_db");
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                long result = db.delete("custom_dialogue", "question=?and answer=?", new String[]{question, answer});
                Log.e("DatabaseUtils", "删除数据：question" + question + "结果--->" + result);

        }
}
